package com.ccp.dev.workflow.service.bpm.impl;


import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.dao.SysUserDao;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.SysUserService;
import com.ccp.dev.workflow.model.BpmNodeUser;
import com.ccp.dev.workflow.model.bpm.CalcVars;
import com.ccp.dev.workflow.model.bpm.PreViewModel;
import com.ccp.dev.workflow.model.bpm.TaskExecutor;
import com.ccp.dev.workflow.service.bpm.BpmNodeUserUtil;
import com.ccp.dev.workflow.service.bpm.IBpmNodeUserCalculation;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 根据节点用户设置为“用户”，计算执行人员。
 * @author jiayueqiao
 */
@Component
public class BpmNodeUserCalculationUser implements IBpmNodeUserCalculation {
	@Resource
	private SysUserService sysUserService;

	@Override
	public List<SysUser> getExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		List<SysUser> sysUsers = new ArrayList<SysUser>();
		String uids = bpmNodeUser.getCmpIds();
		if (StringUtil.isEmpty(uids)) {
			return sysUsers;
		}
		String[] aryUid = uids.split("[,]");
		
		for(String userId : aryUid){
			SysUser user = sysUserService.getOneById(userId);
			if(BeanUtils.isNotEmpty(user)){
				sysUsers.add(user);
			}
		}
		return sysUsers;
	}

	@Override
	public String getTitle() {
		return "用户";
	}
	@Override
	public Set<TaskExecutor> getTaskExecutor(BpmNodeUser bpmNodeUser, CalcVars vars) {
		int extraceUser=bpmNodeUser.getExtractUser();
		List<SysUser> sysUsers = this.getExecutor(bpmNodeUser, vars);
		Set<TaskExecutor> uIdSet=BpmNodeUserUtil.getExcutorsByUsers(sysUsers, extraceUser);
		return uIdSet;
	}

	@Override
	public boolean supportMockModel() {
		return false;
	}

	@Override
	public List<PreViewModel> getMockModel(BpmNodeUser bpmNodeUser) {
		

		return null;
	}

	@Override
	public boolean supportPreView() {
		return true;
	}

	
	
	
}